package cn.zzp.user.controller;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.util.JsonResult;
import cn.zzp.user.dto.LoginDto;
import cn.zzp.user.dto.WxBinderDto;
import cn.zzp.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Administrator
 * @description: 登录
 * @date 2022/7/30 12:08
 */

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 管理员登录
     * @return
     */
    @PostMapping("/account")
    public JsonResult adminLogin(@RequestBody LoginDto loginDto){
        try {
            return loginService.adminLogin(loginDto);
        }catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 微信登录
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable("code") String code){

        try {
            return loginService.wechatLogin(code);
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }

    }

    @PostMapping("/wechat/binder")
    public JsonResult binder(@RequestBody WxBinderDto wxBinderDto){
        try {
            Map<String, Object> map = loginService.binder(wxBinderDto);
            return JsonResult.me().setObj(map);
        }catch (BusinessException e){
            return JsonResult.me(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }


}
